#include "loginwidget.h"
#include "ui_loginwidget.h"
#include<QDebug>
#include<QMessageBox>
#include<QFile>

LoginWidget::LoginWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LoginWidget)
{
    ui->setupUi(this);
    this->ui->userName->hide();//一开始编辑框是隐藏的
    this->ui->userCode->hide();
    this->ui->confirmLog->hide();
    this->ui->confirmReg->hide();
}


LoginWidget::~LoginWidget()
{
    delete ui;
}


void LoginWidget::on_login_clicked()
{
    this->ui->login->hide();//三个按钮隐藏
    this->ui->visitor->hide();
    this->ui->register_2->hide();

    this->ui->userName->show();   //登陆编辑框,确认登陆按钮显示
    this->ui->userCode->show();
    this->ui->confirmLog->show();
}

void LoginWidget::on_visitor_clicked()  //文件中第一行的账号密码为游客进入游戏
{

    gameview.userName="visitor";  //把账号传输给gameview的username
    gameview.show();   //进入游戏
    this->hide();
}

void LoginWidget::on_register_2_clicked()
{
    this->ui->login->hide();//三个按钮隐藏
    this->ui->visitor->hide();
    this->ui->register_2->hide();

    this->ui->userName->show();   //登陆编辑框,确认按钮显示
    this->ui->userCode->show();
    this->ui->confirmReg->show();
}

void LoginWidget::on_confirmLog_clicked()    //登陆确认键。读取账号密码并比对，fileread
{
    QString logname = ui->userName->toPlainText();
    QString logpwd = ui->userCode->toPlainText();
    QString conbineNamePwd =ui->userName->toPlainText().append("\t\t").append(ui->userCode->toPlainText());


    //打开txt
    if((!logname.isEmpty())&&(!logpwd.isEmpty()))   //限制，输入不得为空
    {
        fileContent=FileProcess::fileRead(logAddress);


        //将输入值与文件数据对比
        int flag=0;
        for(flag=0;flag<fileContent.size();flag++)
        {
            if(fileContent[flag]==conbineNamePwd)
            {
                gameview.userName = logname;   //把账号传输给gamewidget的username
                gameview.show();   //账号密码正确，进入游戏
                this->hide();
                break;
            }
        }
        if(flag == fileContent.size())  //查找到最后也没有找到账号密码
        {QMessageBox::information(this, "warning", "Login failed. please check or register first!",
                                                          QMessageBox::Ok);
        }
    }
    else {  //限制，输入不得为空
        QMessageBox::information(this, "warning", "The input must not be empty,retry!",
                                                                  QMessageBox::Ok);
    }


}




void LoginWidget::on_confirmReg_clicked()   //注册确认键，将账号密码写入txt,writeData
{
    QString regname,regpwd,checkRepeat;
    regname = ui->userName->toPlainText();
    regpwd  = ui->userCode->toPlainText();
    checkRepeat = ui->userName->toPlainText().append("\t\t").append(ui->userCode->toPlainText());
    fileContent=FileProcess::fileRead(logAddress);  //读取文件内容，注册的时候不能重复

    if((!regname.isEmpty())&&(!regpwd.isEmpty()))
    {
        //设置标记符，有重复，则标记符不为0
        int count1=0;
        for (int i =0;i<fileContent.size();i++)
        {
            if(fileContent[i]==checkRepeat)
            {count1++;}
        }
        //没有重复注册，则写入文件
        if(count1 ==0)
        { //这里没有使用fileprocess里面的filewrite，因为会覆盖
            QString fileName = logAddress;
            QFile file(fileName);
            if(file.open(QIODevice::WriteOnly|QIODevice::Text|QIODevice::Append))
            {
                QTextStream stream(&file);
                stream<<"\n"<<checkRepeat;
                QMessageBox::information(this, "register", "Registration success",QMessageBox::Ok);
                this->ui->confirmReg->hide();
                this->ui->confirmLog->show();
                file.close();
            }
        }else  //已经有重复账号密码
         {
            QMessageBox::information(this, "warning", "repeat",QMessageBox::Ok);
         }
    }
    else {  //限制，输入不得为空
        QMessageBox::information(this, "warning", "The input must not be empty,retry!",
                                                                  QMessageBox::Ok);
    }



//    QString fileName = "C:\\Users\\wangs\\Documents\\untitled\\day.txt";
//    QFile file(fileName);
//    if(file.open(QIODevice::WriteOnly|QIODevice::Text|QIODevice::Append))
//      {
//          QTextStream stream(&file);



//          //QString conbine = ui->userName->toPlainText().append(ui->userCode->toPlainText());
//          if((!regname.isEmpty())&&(!regpwd.isEmpty()))
//          {
//              stream<<"\n"<<ui->userName->toPlainText()<<"\t\t"<<ui->userCode->toPlainText();
//                            QMessageBox::information(this, "register", "Registration success",
//                                                                      QMessageBox::Ok);
//          }


//          int flag1 =0;
//          qDebug()<<fileContent.size();
//          for(int i=0;i<fileContent.size();i++)
//          {
//              fileContent[i];
//              if(fileContent[i]==conbine)
//              {
//                  flag++;   //这里进不来，隐藏filecontent是空的
//              }
//          }
//          qDebug()<<flag1;
//          if(flag1>0)
//          {
//              stream<<"\n"<<ui->userName->toPlainText()<<"\t\t"<<ui->userCode->toPlainText();
//              QMessageBox::information(this, "register", "Registration success",
//                                                        QMessageBox::Ok);
//          }
//          else
//          {QMessageBox::information(this, "register", "name already exists,retry!",
//                                                        QMessageBox::Ok);}


//          file.close();
//          this->ui->confirmReg->hide();
//          this->ui->confirmLog->show();
//      }
}
